package com.hebeu.gfh.service.impl;

import com.hebeu.gfh.mapper.UserMapper;
import com.hebeu.gfh.mapper.activityApplicationMapper;
import com.hebeu.gfh.pojo.po.Activities;
import com.hebeu.gfh.pojo.po.ActivityApplication;
import com.hebeu.gfh.pojo.po.ActivityRegistrations;
import com.hebeu.gfh.pojo.po.ClubApplication;
import com.hebeu.gfh.pojo.vo.IdAndName;
import com.hebeu.gfh.service.activityApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@Service("activityApplicationService")
public class activityApplicationServiceImpl implements activityApplicationService {
    @Autowired
    activityApplicationMapper applicationMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public void submitApplication(ActivityApplication application) {
        applicationMapper.submitApplication(application);
    }

    @Override
    public List<ActivityApplication> getApplicationByuserId(int userId) {
        List<ActivityApplication> applications =applicationMapper.getApplicationByuserId(userId);
        return applications;
    }

    @Override
    public void approveApplication(int applicationId, int applicationState) {
        applicationMapper.approveApplication(applicationId,applicationState);
    }

    @Override
    public int isApplicated(int userId, int activityId) {
        int sum = applicationMapper.isApplicated(userId,activityId);
        return sum;
    }

    @Override
    public void passApplication(int applicationId) {
        applicationMapper.passApplication(applicationId);
        Activities activities =applicationMapper.getApplicaitonById(applicationId);
        ActivityRegistrations activityRegistrations = new ActivityRegistrations();
        activityRegistrations.setActivityId(activities.getActivityId());
        activityRegistrations.setUserId(activities.getUserId());
        applicationMapper.addRegistration(activityRegistrations);
    }

    @Override
    public List<ActivityApplication> getApplicationByUserId(IdAndName idAndName) {
        int uid;
        try {
            uid = userMapper.findUserIdByUserName(idAndName.getUserName() );
        }catch (Exception e){
            uid =0;
            List<ActivityApplication> clubApplicationList = applicationMapper.getApplicationByUserId(idAndName.getUserId(),uid);
            return clubApplicationList;
        }
        List<ActivityApplication> clubApplicationList = applicationMapper.getApplicationByUserId(idAndName.getUserId(),uid);
        return clubApplicationList;
    }

    @Override
    public void unpassApplication(int applicationId) {
        applicationMapper.unpassApplication(applicationId);
    }

    @Override
    public List<ActivityApplication> getActivityApplicationByUserId(int userId) {
        List<ActivityApplication> applicationList = applicationMapper.getActivityApplicationByUserId(userId);
        return applicationList;
    }
}
